"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _antdV = require("antd-v4");

var _react = require("react");

require("./index.less");

var _excluded = ["label", "rules", "name", "children", "popoverProps"],
    _excluded2 = ["errorType", "rules", "name", "popoverProps", "children"];
var FIX_INLINE_STYLE = {
  marginTop: -5,
  marginBottom: -5,
  marginLeft: 0,
  marginRight: 0
};

var InlineErrorFormItem = function InlineErrorFormItem(_ref) {
  var inputProps = _ref.inputProps,
      input = _ref.input,
      extra = _ref.extra,
      errorList = _ref.errorList,
      popoverProps = _ref.popoverProps;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      visible = _useState2[0],
      setVisible = _useState2[1];

  var _useState3 = (0, _react.useState)([]),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      errorStringList = _useState4[0],
      setErrorList = _useState4[1];

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  (0, _react.useEffect)(function () {
    if (inputProps.validateStatus !== 'validating') {
      setErrorList(inputProps.errors);
    }
  }, [inputProps.errors, inputProps.validateStatus]);
  var prefixCls = getPrefixCls();
  return (0, _jsxRuntime.jsx)(_antdV.Popover, (0, _objectSpread2.default)((0, _objectSpread2.default)({
    trigger: (popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.trigger) || 'focus',
    placement: (popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.placement) || 'topRight',
    visible: errorStringList.length < 1 ? false : visible,
    onVisibleChange: function onVisibleChange(visibleParams) {
      if (visibleParams === visible) return;
      setVisible(visibleParams);
    },
    getPopupContainer: popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.getPopupContainer,
    getTooltipContainer: popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.getTooltipContainer,
    content: (0, _jsxRuntime.jsxs)("div", {
      className: "".concat(prefixCls, "-form-item-with-help"),
      children: [inputProps.validateStatus === 'validating' ? (0, _jsxRuntime.jsx)(_icons.LoadingOutlined, {}) : null, errorList]
    })
  }, popoverProps), {}, {
    children: (0, _jsxRuntime.jsxs)("div", {
      children: [input, extra]
    })
  }), "popover");
};

var InternalFormItem = function InternalFormItem(_ref2) {
  var label = _ref2.label,
      rules = _ref2.rules,
      name = _ref2.name,
      children = _ref2.children,
      popoverProps = _ref2.popoverProps,
      rest = (0, _objectWithoutProperties2.default)(_ref2, _excluded);
  return (0, _jsxRuntime.jsx)(_antdV.Form.Item, (0, _objectSpread2.default)((0, _objectSpread2.default)({
    preserve: false,
    name: name,
    rules: rules,
    hasFeedback: true,
    // @ts-ignore
    _internalItemRender: {
      mark: 'pro_table_render',
      render: function render(inputProps, doms) {
        return (0, _jsxRuntime.jsx)(InlineErrorFormItem, (0, _objectSpread2.default)({
          inputProps: inputProps,
          popoverProps: popoverProps
        }, doms));
      }
    }
  }, rest), {}, {
    style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, FIX_INLINE_STYLE), rest === null || rest === void 0 ? void 0 : rest.style),
    children: children
  }));
};

var _default = function _default(props) {
  var errorType = props.errorType,
      rules = props.rules,
      name = props.name,
      popoverProps = props.popoverProps,
      children = props.children,
      rest = (0, _objectWithoutProperties2.default)(props, _excluded2);

  if (name && (rules === null || rules === void 0 ? void 0 : rules.length) && errorType === 'popover') {
    return (0, _jsxRuntime.jsx)(InternalFormItem, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      name: name,
      rules: rules,
      popoverProps: popoverProps
    }, rest), {}, {
      children: children
    }));
  }

  return (0, _jsxRuntime.jsx)(_antdV.Form.Item, (0, _objectSpread2.default)((0, _objectSpread2.default)({
    rules: rules
  }, rest), {}, {
    style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, FIX_INLINE_STYLE), rest.style),
    name: name,
    children: children
  }));
};

exports.default = _default;